package model;

import java.util.*;
import sql.SqlConnector;

/**
 * 11-01-2010
 * 
 * This is the class in charge of all the category information 
 * When an appointment has been added/changed/deleted
 * notifies all observers
 * 
 * Currently there is no abstract class to define the storage
 * of the data, it only accepts the sqlconnector class.
 * 
 * @author Timo Janssen
 */
public class CategoryModel extends Observable {
	private SqlConnector connector;

	/**
	 * Constructor, needs to know the SqlConnector for the data
	 * storage and manupulation.
	 * 
	 * @param newConnector
	 */
	public CategoryModel(SqlConnector newConnector) {
		connector = newConnector;
	}

	/**
	 * Returns the requested category.
	 * 
	 * @param id The ID of the existing category
	 * @throws nullpointerexception on unexisting category ID
	 * @return The category
	 */
	public CategoryDataModel getCategory(int id){
		if(connector.categoryExists(id)) {
			return connector.selectCategory(id);
		} else {
			throw new NullPointerException();
		}
	}

	/**
	 * Get all the categories from database
	 * 
	 * @return All the categories
	 */
	public Vector<CategoryDataModel> getAllCategories(){
		return connector.selectAllCategories();
	}

	/** Adds a new category to the database
	 * 
	 * @param title
	 */
	public void addCategory( String title ){
		connector.addCategory(title);
		setChanged();
		notifyObservers();		
	}

	/** 
	 * Edits an existing category in the database.
	 * 
	 * @param id The category id to edit
	 * @param title The new title
	 */
	public void editCategory(int id, String title){
		if(connector.categoryExists(id)){
			connector.updateCategory(id, title);
			setChanged();
			notifyObservers();
		} else {
			throw new NullPointerException();
		}
	}

	/** 
	 * Deletes an existing category in the database.
	 * 
	 * @param id
	 * @throws nullpointerexception on unexisting category ID
	 */
	public void deleteCategory(int id) {
		if(connector.categoryExists(id)) {
			connector.deleteCategory(id);
			setChanged();
			notifyObservers();
		} else {
			throw new NullPointerException();
		}	
	}
}